package controller;


import com.google.gson.Gson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

import domain.Question;
import domain.Return;
import domain.Student;
import domain.TestJava;
import service.QuestionServiceImp;
import service.StudentServiceImp;
import service.TestJavaServiceImp;
import utils.GsonUtils;

/**
 * User: xinxin
 * Date: 2016-11-05
 * Time: 20:59
 * FIXME
 */
@Controller
@RequestMapping("/le/json")
public class IntelligenceExamController {
    private final int EXAM_QUESTION_COUNT = 25;
    @Autowired
    StudentServiceImp mStudentServiceImp;
    @Autowired
    TestJavaServiceImp mTestJavaServiceImp;
    @Autowired
    QuestionServiceImp mQuestionServiceImp;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Return login(@RequestParam(value = "student_code") String code, @RequestParam("password") String password) {
        final Student stu = mStudentServiceImp.login(code, password);
        if (stu != null) {
            return Return.commonReturn().setData(stu);
        }
        return Return.faliReturn();
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public Return register(@RequestParam("student_json") String studentJson) {
        System.out.println(studentJson);
        final Student stu = (Student) GsonUtils.fromJsonString(studentJson,Student.class);
        if (stu != null) {
            if(!mStudentServiceImp.checkCode(stu.getCode())){
                return Return.faliReturn().setMesg("用户账号已经存在，请更换");
            }
            if (mStudentServiceImp.register(stu)) {
                return Return.successReturn();
            } else {
                return Return.faliReturn();
            }
        }
        return Return.faliReturn();
    }

    @RequestMapping(value = "/random_questions", method = RequestMethod.GET)
    @ResponseBody
    public Return getRandomQuestions(@RequestParam("question_count") int questionCount) {
        final List<Question> randomQuestions = mQuestionServiceImp.getRandomQuestions(questionCount);
        if (randomQuestions != null) {
            return Return.commonReturn().setData(randomQuestions);
        }
        return Return.faliReturn();
    }

    /**
     * 获取考试专用的试题，默认试题数为25题
     *
     * @return
     */
    @RequestMapping(value = "/exam_questions", method = RequestMethod.GET)
    @ResponseBody
    public Return getExamQuestions(@RequestParam(value = "question_count", required = false, defaultValue = "25") int questionCount) {
        final List<Question> randomQuestions = mQuestionServiceImp.getRandomQuestions(questionCount);
        if (randomQuestions != null) {
            return Return.commonReturn().setData(randomQuestions);
        }
        return Return.faliReturn();
    }

    @RequestMapping(value = "/exam_info_all", method = RequestMethod.GET)
    @ResponseBody
    public Return getExamInfos(@RequestParam("student_id") int studentId) {
        final List<TestJava> testResults = mTestJavaServiceImp.getTestResultByStudentId(studentId);
        if (testResults != null) {
            return Return.commonReturn().setData(testResults);
        }
        return Return.faliReturn();
    }

    @RequestMapping(value = "/post_exam_info", method = RequestMethod.POST)
    @ResponseBody
    public Return addExamInfo(@RequestParam("student_id") int studentId, @RequestParam("grade") float grade) {
        TestJava testJava = new TestJava();
        testJava.setStudentId(studentId);
        testJava.setTestDate(new Date());
        testJava.setGrade(grade);
        if (mTestJavaServiceImp.addTestResult(testJava)) {
            return Return.successReturn();
        }
        return Return.faliReturn();
    }
}
